package com.blb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blb.entity.*;
import com.blb.mapper.SalaryMapper;
import com.blb.mapper.StaffMapper;
import com.blb.mapper.UserRoleMapper;
import com.blb.service.IPostService;
import com.blb.service.ISalaryService;
import com.blb.service.IStaffService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blb.service.IUserService;
import com.blb.utils.Response;
import com.blb.utils.ShiroUtils;
import com.blb.utils.UUIDUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyx
 * @since 2020-05-25
 */
@Service
public class StaffServiceImpl extends ServiceImpl<StaffMapper, Staff> implements IStaffService {

    @Resource
    StaffMapper staffMapper;

    @Autowired
    IPostService postService;

    @Autowired
    ISalaryService iSalaryService;
    @Resource
    UserRoleMapper userRoleMapper;

    @Autowired
    IUserService userService;
    @Override
    public List<Dept> xiala(String companyId) {
        return staffMapper.xiala(companyId);
    }

    @Override
    public List<Post> postName(String id,String companyId) {
        return staffMapper.postName(id,companyId);
    }

    @Override
    public Integer add(Staff staff) {
        return staffMapper.add(staff);
    }

    @Transactional
    @Override
    public Response addAll(Staff staff,String status) {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        String staffId = UUIDUtils.getId();
        //员工添加
        staff.setId(staffId);
        staff.setStaffStatus("0");
        staff.setNumberId("1");
        staff.setCompanyId(user.getId());

        //员工薪水添加
        Salary salary = new Salary();
        String checkedIds[]=status.split(",");
        salary.setCompanyId(user.getId());
        salary.setSalaryStatus(staff.getStaffId());

        //用户表添加员工账号密码
        User u = new User();
        u.setUserAccount(staff.getStaffUsername());
        u.setUserPassword(staff.getStaffPwd());
        u.setUserStatus("Y");
        ShiroUtils.getPass(u);
        u.setId(UUIDUtils.getId());
        u.setCompanyId(user.getId());
        u.setStaffId(staffId);

        //员工薪水表添加数据
        for (String s:checkedIds) {
            if(s.equals("1")){
                salary.setSalaryBase("1");
            } if(s.equals("2")){
                salary.setSalaryMerit("2");
            } if(s.equals("3")){
                salary.setSalaryWork("3");
            } if(s.equals("4")){
                salary.setSalarySubsidy("4");
            } if(s.equals("5")){
                salary.setSalaryFive("5");
            }
        }

        List<Staff> isStaffId = isStaffId(staff.getStaffId(),user.getId());
        if(isStaffId.isEmpty()){
            List<Staff> staffList = isStaffName(staff.getStaffUsername());
            if(staffList.isEmpty()){
                int i = addUser(u);
                if(i>0){
                    int b =add(staff);
                    if(b>0){
                        iSalaryService.save(salary);
                        return Response.success();
                    }else{
                        return Response.error("员工添加错误");
                    }
                }else {
                    return Response.error("用户添加错误");
                }
            }else{
                return Response.error("员工账户已存在");
            }
        }else{
            return Response.error("员工编号已存在");
        }
    }

    @Override
    public List<Staff> staffList(@Param("start") int start, @Param("length")int length, @Param("userName")String userName,@Param("companyId") String companyId) {
        return staffMapper.staffList(start,length,userName,companyId);
    }

    @Override
    public Integer staffAll(String companyId) {
        return staffMapper.staffAll(companyId);
    }

    @Override
    public Staff staffUpd(@Param("id")String id,@Param("companyId")String companyId) {
        return staffMapper.staffUpd(id,companyId);
    }

    @Override
    public List<StaffStatus> status(String companyId) {
        return staffMapper.status(companyId);
    }

    @Override
    public Integer updStatus(Staff staff) {
        return staffMapper.updStatus(staff);
    }

    @Transactional
    @Override
    public void updStaffAll(String status, Staff staff) {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        staff.setCompanyId(user.getId());
        Salary salary = new Salary();
        salary.setCompanyId(user.getId());
        salary.setSalaryStatus(staff.getStaffId());
        String checkedIds[]=status.split(",");
        for (String s:checkedIds) {
            if(s.equals("1")){
                salary.setSalaryBase("1");
            } if(s.equals("2")){
                salary.setSalaryMerit("2");
            } if(s.equals("3")){
                salary.setSalaryWork("3");
            } if(s.equals("4")){
                salary.setSalarySubsidy("4");
            } if(s.equals("5")){
                salary.setSalaryFive("5");
            }
        }
        staffMapper.updStaff(staff);
        staffMapper.updSalary(salary);

    }

    /**
     * 查询员工信息
     * @param companyId
     * @return
     */
    @Override
    public List<Staff> selectStaffBycompanyId(String companyId) {
        return staffMapper.selectStaffBycompanyId(companyId);
    }
    /**
     * 绑定员工角色
     * @param roleId
     * @param staffIds
     */
        @Transactional
        @Override
        public void addStaffRole(String roleId, String[] staffIds) {

        for(String staffid:staffIds){
            User user = userService.getOne(new QueryWrapper<User>().eq("staff_id",staffid));
            userRoleMapper.delete(new QueryWrapper<UserRole>().eq("user_id",user.getId() ));
            UserRole userRole = new UserRole();
            userRole.setId(UUIDUtils.getId());
            userRole.setUserId(user.getId());
            userRole.setRoleId(roleId);
            userRoleMapper.insert(userRole);
            //staffMapper.addStaffRole(roleId,staffid);
        }

    }

    @Override
    public List<Staff> isStaffId(String staffId,String companyId) {
        return staffMapper.isStaffId(staffId,companyId);
    }

    @Override
    public List<Staff> isStaffName(String staffName) {
        return staffMapper.isStaffName(staffName);
    }

    @Override
    public List<Salary> salaryList(String salary) {
        return staffMapper.salaryList(salary);
    }

    @Override
    public Integer addUser(User user) {
        return staffMapper.addUser(user);
    }
}
